package stu.day25_day32.day25.day1;

import stu.day25_day32.day25.all.TreeNode;

import java.util.*;


public class LeetCode107 {
    public List<List<Integer>> levelOrderBottom(TreeNode root) {

        List<List<Integer>> list = new ArrayList<>();
        Queue<TreeNode> queue = new LinkedList<>();

        if(root==null){
            return list;
        }

        queue.offer(root);
        int num = 0;

        while (!queue.isEmpty()){
            List<Integer> l = new ArrayList<>();
            int size = queue.size();
            for(int i=0;i<size;i++){
                TreeNode t = queue.poll();
                if(t.left!=null){
                    queue.offer(t.left);
                }
                if(t.right!=null){
                    queue.offer(t.right);
                }
                l.add(t.val);
            }
            if(num%2==1){
                Collections.reverse(l);
            }
            list.add(l);
            num += 1;
        }

        return list;


    }

}
